// pages/thief/result/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    resultImage: '/images/success-result.png',
    resultTitle: '偷取结果',
    resultMessage: '',
    success: false,
    targetMood: null,
    thief: null,
    showHearts: false,
    hearts: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('Result page loaded with options:', options);
    
    // 从本地存储获取结果数据
    const resultData = wx.getStorageSync('last_steal_result');
    
    if (resultData) {
      this.setData({
        success: resultData.success,
        targetMood: resultData.targetMood,
        thief: resultData.thief,
        resultTitle: resultData.success ? '偷取成功' : '偷取失败',
        resultMessage: resultData.success 
          ? `恭喜你！${resultData.thief.name}成功偷取了${resultData.targetMood.emoji}${resultData.targetMood.name}心情！`
          : `${resultData.thief.name}没能成功偷取${resultData.targetMood.name}，不过不要灰心，再试一次吧！`
      });
      
      if (resultData.success) {
        this.setData({
          resultImage: '/images/success-result.png'
        });
        this.showHeartsAnimation();
      } else {
        this.setData({
          resultImage: '/images/failure-result.png'
        });
      }
    } else {
      // 默认显示
      this.setData({
        resultTitle: '偷取结果',
        resultMessage: '暂无结果数据',
        resultImage: '/images/failure-result.png'
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    // 页面准备完成
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 页面显示时检查是否有新的结果数据
    const resultData = wx.getStorageSync('last_steal_result');
    if (resultData) {
      this.onLoad(); // 重新加载数据
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  // 显示爱心动画
  showHeartsAnimation() {
    const hearts = [];
    for (let i = 0; i < 20; i++) {
      hearts.push({
        id: i,
        left: Math.random() * 300,
        top: Math.random() * 200,
        delay: Math.random() * 2
      });
    }
    
    this.setData({
      showHearts: true,
      hearts: hearts
    });
  },

  // 返回首页
  goHome() {
    wx.redirectTo({
      url: '/pages/thief/select/index'
    });
  },

  // 再试一次
  tryAgain() {
    wx.redirectTo({
      url: '/pages/thief/select/index'
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: this.data.success 
        ? `我用${this.data.thief.name}成功偷取了${this.data.targetMood.name}心情！`
        : `虽然这次没成功，但我还会再来的！`,
      path: '/pages/thief/select/index',
      imageUrl: this.data.resultImage
    };
  }
})